package com.iamgod.savemoney.plan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.iamgod.savemoney.R;
import com.iamgod.savemoney.base.BaseActivity;
import com.iamgod.savemoney.bean.PlanConfigBean;
import com.iamgod.savemoney.dao.DayPlanDao;
import com.iamgod.savemoney.dao.PlanConfigDao;
import com.iamgod.savemoney.sys.Config;

/**
 * 日计划列表
 * 
 * @author llj
 * 
 */
public class PlanListAct extends BaseActivity {

	TextView nav_tit,tv_foot,tv_head;// 顶部导航标题
	ListView listView;// 数据列表
	SimpleAdapter listAdater;
	LinearLayout footer;
	LinearLayout header;
	// 列表项
	static String ID = "id";
	static String DATE = "date";
	static String CONTENT = "content";
	// 存值对象
	List<HashMap<String, Object>> list;
	PlanConfigDao dao;

	static String TAG = "PlanListAct";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.plan_list_act);
		initNav(this);
		initUI();
		new Thread(initDataThread).start();

	}

	void initUI() {
		nav_tit = (TextView) this.findViewById(R.id.tv_tit);
		listView = (ListView) this.findViewById(R.id.lv_list);
		
		//列表项监听
		listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Log.i(TAG, "点击项---"+position);
				if(position!=0&&position!=list.size()+1){
					Log.i(TAG, "点击内容---"+list.get(position-1).get("content"));
					Intent i = new Intent();
					i.putExtra("id", list.get(position-1).get(ID).toString());
					i.setClass(PlanListAct.this, UpdatePlanAct.class);
					startActivity(i);
				}else if(position==list.size()+1){
					Intent i = new Intent();
					i.putExtra("planType", "常规消费");
					i.setClass(PlanListAct.this, SetPlanAct.class);
					startActivity(i);
				}
				
			}
			
		});
		
		listView.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					final int position, long id) {
				Log.i(TAG, "点击项---"+position);
				if(position!=0&&position!=list.size()+1){
					Log.i(TAG, "点击内容---"+list.get(position-1).get("content"));
					new AlertDialog.Builder(PlanListAct.this)
					.setTitle("操作提示")
					.setMessage("确定要删除此计划")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int whichButton) {
									dao = new PlanConfigDao(PlanListAct.this,Config.DataBase.DATABASE_NAME);
									dao.delete(Long.parseLong(list.get(position-1).get("id").toString()));
									dao.close();
									list.remove(position-1);
									listAdater.notifyDataSetChanged();
								}
							})
					.setNegativeButton("取消", null)
					.show();
				}
				return false;
			}
			
		});

	}
	
	void putData(){

		if (listAdater == null) {
			listAdater = new SimpleAdapter(PlanListAct.this, list,
					R.layout.list_item, new String[] { DATE,
							CONTENT }, new int[] { R.id.tv_date,
							R.id.tv_content });
			//添加底部和顶部
			footer = (LinearLayout) LayoutInflater.from(PlanListAct.this).inflate(
					R.layout.foot_item, null);
			header = (LinearLayout) LayoutInflater.from(PlanListAct.this).inflate(
					R.layout.head_item, null);
			
			tv_foot = (TextView) footer.findViewById(R.id.tv_list_foot);
			tv_foot.setText("点击此项新增一个计划设置");
			tv_head = (TextView) header.findViewById(R.id.tv_list_head);
			tv_head.setText("美好生活源自合理的计划");
			
			listView.addFooterView(footer);
			listView.addHeaderView(header);
			
			listView.setAdapter(listAdater);
		} else {
			listAdater.notifyDataSetChanged();
		}
	
	}

	Handler handle = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				if (list != null && list.size() > 0) {
					putData();
				}
				break;
			}
			super.handleMessage(msg);
		}

	};

	Thread initDataThread = new Thread() {

		@Override
		public void run() {
			Message msg = new Message();
			msg.what = 0;
			dao = new PlanConfigDao(PlanListAct.this,
					Config.DataBase.DATABASE_NAME);
//			PlanConfigBean bean = new PlanConfigBean();
//			bean.setPlanName("坐车");
//			bean.setPlanTime("08:30");
//			bean.setPlanPay("2.00");
//			bean.setConsumeType("行");
//			bean.setPlanType("常规消费");
//			dao.insert(bean);
//			bean.setPlanName("中餐");
//			bean.setPlanTime("12:20");
//			bean.setPlanPay("12.00");
//			bean.setConsumeType("食");
//			bean.setPlanType("常规消费");
//			dao.insert(bean);
//			bean.setPlanName("晚餐");
//			bean.setPlanTime("20:20");
//			bean.setPlanPay("12.00");
//			bean.setConsumeType("食");
//			bean.setPlanType("常规消费");
//			dao.insert(bean);
			List<PlanConfigBean> planList = dao.query();
			if (planList != null && planList.size() > 0) {
				list = new ArrayList<HashMap<String, Object>>();
				for (int i = 0; i < planList.size(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put(ID, planList.get(i).getId());
					map.put(CONTENT, planList.get(i).getPlanName());
					map.put(DATE, planList.get(i).getPlanTime());
					list.add(map);
				}
			}
			dao.close();
			handle.sendMessage(msg);
			super.run();
		}

	};
}
